﻿using System.Security.Cryptography;
using System.Text;

namespace _29_领域事件发布机制.Models
{
    public class lingyu:BaseEntity
    {
        public Guid Id { get; init; }
        public string UserName { get; init; }
        public string Email { get; private set; }
        public string? NickName { get; private set; }

        public string? Age { get; set; }
        public bool isDeleted { get; private set;  }

        private string passwordHash;

        private lingyu()
        {
            
        }
        public lingyu(string userName,string email)
        {
            this.Id = Guid.NewGuid();
            this.UserName = userName;
            this.Email = email;
            this.isDeleted = false;

            AddDomainEvent(new sendNotification(this));
        }
        public void changePwd(string input)
        {
            string orderpwd = input;
            passwordHash=GetMd5Hash(input);
            AddDomainEventIfAbsent(new sendNotification_update(orderpwd, passwordHash));
        }

        private  string GetMd5Hash(string input)
        {
            using (MD5 md5Hash = MD5.Create())
            {
                byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < data.Length; i++)
                {
                    sb.Append(data[i].ToString("x2"));
                }
                return sb.ToString();
            }
        }
    }


}
